package sw6.digipecs.model;

import sw6.digipecs.database.PictureActiveCursor;
import sw6.digipecs.exceptions.IDNotFoundException;
import sw6.digipecs.exceptions.ImageIsEmptyException;
import android.content.Context;

public class ExtImageInSituation extends ExtImage {

	private boolean mActive = false;
	private int mSituationId;

	public ExtImageInSituation(Context context, int id, int situation_id) throws IDNotFoundException {
		super(context, id);
		
		PictureActiveCursor cursor;
		
		mDB.open();
		cursor = mDB.fetchPictureActive(id, situation_id);
		
		if (cursor.moveToFirst()) {
			mActive = cursor.getPictureActiveState();
			mSituationId = situation_id;
		} else {
			mActive = false;
			
			cursor.close();
			mDB.close();
			throw new IDNotFoundException();
		}
		
		cursor.close();
		mDB.close();
	}
	
	public boolean isActive() throws ImageIsEmptyException {
		if (isEmpty()) {
			throw new ImageIsEmptyException();
		}
		
		return mActive;
	}
	
	public void setActive(boolean status) throws ImageIsEmptyException {
		if (isEmpty()) {
			throw new ImageIsEmptyException();
		}
		
		mDB.open();
		mDB.setPictureActiveState(getId(), mSituationId, status);
		mDB.close();
		mActive = status;
	}

}
